package com.leonyr.smartipaddemo.crop;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.leonyr.smartipaddemo.R;
import com.serenegiant.usb.Size;

import java.util.ArrayList;
import java.util.List;

public class ReviewSizeFragment extends DialogFragment {
    private RadioGroup rgSize;
    private OnReviewSizeChangedListener listener;
    private ArrayList<String> sList;
    private ArrayList<Size> iList;
    private int height;
    private int width;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        listener = (OnReviewSizeChangedListener) context;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定

        dialog.setCanceledOnTouchOutside(true);
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.dimAmount = 0.6f;
            window.setAttributes(lp);
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fm_review_size, container,false);
        iniData();
        initView(view);
        return view;
    }

    private void iniData() {
        sList = getArguments().getStringArrayList("slist");
        iList = getArguments().getParcelableArrayList("ilist");
        height = getArguments().getInt("height");
        width = getArguments().getInt("width");
    }

    private void initView(View view) {
        rgSize = view.findViewById(R.id.rg_size);
        if(sList!=null&&sList.size()>0){
            for(int i=0;i<sList.size();i++){
                createRadioButton(i,sList.get(i));
            }
        }
        if(sList!=null){}
        rgSize.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id) {
                RadioButton rb=rgSize.findViewById(id);
                int index= (int) rb.getTag();
                listener.onSizeChanged(index);
            }
        });
    }

    private void createRadioButton(int index,String text) {
        RadioButton rb = new RadioButton(getContext());
        rb.setText(text);
        rb.setTextColor(getResources().getColor(R.color.white));
        rb.setTextSize(16);
        rb.setTag(index);
        rgSize.addView(rb);
        if(index==0){
            rgSize.check(rb.getId());
            rb.setChecked(true);
        }
        Size size=iList.get(index);
        if(size.height == height&&size.width == width){
            rgSize.check(rb.getId());
        }
    }

    public interface OnReviewSizeChangedListener{
        void onSizeChanged(int index);
    }

    public static ReviewSizeFragment newInstance(List<String> sizeStrList, List<Size> sizeiList, int height, int width){
        Bundle data = new Bundle();
        data.putStringArrayList("slist", (ArrayList<String>) sizeStrList);
        data.putParcelableArrayList("ilist", (ArrayList<Size>) sizeiList);
        data.putInt("width",width);
        data.putInt("height",height);
        ReviewSizeFragment fragment = new ReviewSizeFragment();
        fragment.setArguments(data);
        return fragment;
    }
}
